<?php
/**
 * mdkeji_chatfree模块PC接口定义
 *
 * @author Mob990561601898
 * @url
 */
defined('IN_IA') or exit('Access Denied');
if (!function_exists('M')) {
    function M($name)
    {
        static $model = array();
        if (empty($model[$name])) {
            include '../addons/mdkeji_chatfree/model/' . $name . '.mod.php';
            $model[$name] = new $name();
        }
        return $model[$name];
    }
}

class Mdkeji_chatfreeModuleWebapp extends WeModuleWebapp {

	public function __construct(){

		session_start();
		global $_W,$_GPC;

		if(empty($_SESSION['openid'])
		&&!empty($_COOKIE['openid'])){
			$_SESSION['openid']=$_COOKIE['openid'];
		}
	}


	public function doPageTest(){
		global $_GPC, $_W;
		$errno = 0;
		$message = '返回消息';
		$data = array();
		return $this->result($errno, $message, $data);
	}

	public function doPageIndex() {
		session_start();
		global $_W,$_GPC;

		if(isset($_GET['nickName'])){
			$nickName=$_GET['nickName'];

			$userResult=M('user')->existNickName($nickName);
			if($userResult>0){
				$_SESSION['openid']=$userResult['openid'];
			}else{
				$openid=$this->create_guid();
				$nickName=$_GET['nickName'];
				$avatarUrl="http://".$_SERVER['HTTP_HOST']."/addons/mdkeji_chatfree/public/images/user/user".rand(1,70).".png";
				$password='123456';
				$timein=date("Y-m-d H:i:s");

				if(isset($openid)&&isset($nickName)&&isset($avatarUrl)&&isset($password)){

					$i=M("user")->insertUser(['openid'=>$openid,'password'=>$password,'avatarUrl'=>$avatarUrl,'nickName'=>$nickName,'timein'=>$timein]);
					if($i>0){
						$_SESSION['openid']=$openid;
					}

				}
			}
		}

	//生成cookie，腾讯微信浏览器不支持安卓端session跳转，丢失。
	if(!empty($_SESSION['openid'])){
		setcookie("openid", $_SESSION['openid'], time()+36000);
	}

		$domain=M('domains')->get(array('Id'=>1));
		include $this->template("index");
	}

}